#![allow(dead_code)]

struct IndexLrn {}

impl IndexLrn {
    fn hello() {
        println!("Index learnning!");
    }
}

#[cfg(test)]
mod tests {
    // use super::*;

    use std::ops::{Index, IndexMut};

    #[test]
    fn index_and_index_mut() {
        #[derive(Debug)]
        enum Side {
            Left,
            Right,
        }

        #[derive(Debug, PartialEq)]
        enum Weight {
            Kilogram(f32),
            Pound(f32),
        }

        struct Balance {
            pub left: Weight,
            pub right: Weight,
        }

        impl Index<Side> for Balance {
            type Output = Weight;

            fn index(&self, index: Side) -> &Self::Output {
                println!("Accessing {index:?}-side of balance immutably");
                match index {
                    Side::Left => &self.left,
                    Side::Right => &self.right,
                }
            }
        }

        impl IndexMut<Side> for Balance {
            fn index_mut(&mut self, index: Side) -> &mut Self::Output {
                println!("Accessing {index:?}-side of balance mutably");
                match index {
                    Side::Left => &mut self.left,
                    Side::Right => &mut self.right,
                }
            }
        }

        let mut balance = Balance {
            left: Weight::Kilogram(2.5),
            right: Weight::Pound(1.5),
        };

        println!("{:?}", balance.left);

        assert_eq!(balance[Side::Right], Weight::Pound(1.5));

        balance[Side::Left] = Weight::Kilogram(100.3);

        println!("{:?}", balance[Side::Left]);
    }
}
